package com.example.frn.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.example.frn.dao.FilmMapper;
import com.example.frn.dao.RankingListMapper;
import com.example.frn.dto.RankingListPageDto;
import com.example.frn.entity.Film;
import com.example.frn.entity.RankingList;
import com.example.frn.service.RankingListService;
import com.example.frn.vo.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
@Service
public class RankingListServiceImpl implements RankingListService {
    @Autowired
    private RankingListMapper rankingListMapper;
    @Override
    public boolean save(RankingList rankingList) {
        return rankingListMapper.insert(rankingList)>0;
    }

    @Override
    public boolean updateById(RankingList rankingList) {
        return rankingListMapper.updateById(rankingList)>0;
    }

    @Override
    public boolean removeById(Integer id) {
        return rankingListMapper.deleteById(id)>0;
    }

    @Override
    public RankingList getById(Integer id) {
        RankingList rankingList = rankingListMapper.selectById(id);
        rankingList.setFilmList(filmMapper.selectlistId(rankingList.getFilmIdList()));
        return rankingList;
    }
    @Autowired
    private FilmMapper filmMapper;
    @Override
    public List<Film> list(Integer year,String categoryName) {
        List<RankingList> list = rankingListMapper.selectList(year,categoryName);
        if(CollUtil.isNotEmpty(list)){
            RankingList rankingList = list.get(0);
            String filmIdList = rankingList.getFilmIdList();
            if(StrUtil.isNotBlank(filmIdList))
                return filmMapper.selectlistId(filmIdList);
        }
        return new LinkedList<Film>();
    }




    @Override
    public PageInfo<RankingList> page(RankingListPageDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<RankingList> list = rankingListMapper.selectList(dto.getYear(),dto.getCategoryName());
        for (RankingList rankingList : list) {
            rankingList.setFilmList(filmMapper.selectlistId(rankingList.getFilmIdList()));
        }
        return new PageInfo<>(list);
    }
}
